﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Entity.ProjectEnt;
using ZFine.Application.Project;
using ZFine.Application.SystemManage;
using ZFine.Code;
using ZFine.Domain.Entity;
using ZFine.Web;

namespace Logiv.WEB.Areas.Project.Controllers
{
    public class GudaoCarController : BaseController
    {
        private  GudaoCarApp serverApp=new GudaoCarApp();
        private PositionRecordApp positoonapp = new PositionRecordApp();
        public CustomerAndThingsNameApp CTserverApp = new CustomerAndThingsNameApp();
        private CarriageBoxApp carriageBoxApp = new CarriageBoxApp();

        private UserApp userApp = new UserApp();
        private PositionRecordApp posService = new PositionRecordApp();


        [HttpGet]
        public ActionResult ExportExcell(string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "AccomplishPaicheTime desc";
            pagination2.sidx = "AccomplishPaicheTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            var reuslt = serverApp.GetListPass(pagination2, keyword, StartTime, EndTime, carId, customerName);
            var result = (from num in reuslt
                          select new GudaoCarEntity
                          {
                              F_Id = num.F_Id,
                              BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                              {
                                  F_Id = num.BelongCarriage.F_Id,
                                  CarriageID = num.BelongCarriage.CarriageID,
                                  CarType = num.BelongCarriage.CarType,
                                  Capacity = num.BelongCarriage.Capacity
                              },
                              ThingsName = num.ThingsName,
                              Fenhao = num.Fenhao,
                              ArriveStation = num.ArriveStation,
                              UnitNumber = num.UnitNumber,
                              Weight = num.Weight,
                              Customers = num.Customers,
                              isAccomplishStatistics = num.isAccomplishStatistics,
                              IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                              isAccomplishPaiche = num.isAccomplishPaiche,
                              IsAccomplishduiwei = num.IsAccomplishduiwei,
                              AccomplishPaicheTime = num.AccomplishPaicheTime,
                              isQiPiao = num.isQiPiao,
                              ArriveDateTime = num.ArriveDateTime,
                              Remark = num.Remark,
                              BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                              {
                                  F_Id = num.BelongPathway.F_Id,
                                  Name = num.BelongPathway.Name
                              },
                              Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                              {
                                  F_Id = num.Operation.F_Id,
                                  OperationType = num.Operation.OperationType,
                              },
                              QiPiaoOper = num.QiPiaoOper == null ? null : new OperationGudaoCarEntity()
                              {
                                  F_Id = num.QiPiaoOper.F_Id,
                                  OperationType = num.QiPiaoOper.OperationType,
                                  Banci = num.QiPiaoOper.Banci,
                                  OperationTime = num.QiPiaoOper.OperationTime,
                                  OperationPersion = num.QiPiaoOper.OperationPersion == null ? null : new UserEntity()
                                  {
                                      F_Id = num.QiPiaoOper.OperationPersion.F_Id,
                                      F_RealName = num.QiPiaoOper.OperationPersion.F_RealName
                                  }
                              },
                              PassOper = num.PassOper == null ? null : new OperationGudaoCarEntity()
                              {
                                  F_Id = num.PassOper.F_Id,
                                  OperationType = num.PassOper.OperationType,
                              },
                              ClearGudaoOper = num.ClearGudaoOper == null ? null : new OperationGudaoCarEntity()
                              {
                                  F_Id = num.ClearGudaoOper.F_Id,
                                  IsRollBack = num.ClearGudaoOper.IsRollBack
                              },
                              CarGudaoIndex = num.CarGudaoIndex
                          }).ToList();
            //排序
            List<GudaoCarEntity> list = new List<GudaoCarEntity>();
            List<GudaoCarEntity> list2 = new List<GudaoCarEntity>();
            List<string> idlList = new List<string>();
            string fid = "";
            foreach (var item in result)
            {
                if (item.ClearGudaoOper.F_Id != fid)
                {
                    fid = item.ClearGudaoOper.F_Id;
                    idlList.Add(item.ClearGudaoOper.F_Id);
                }
            }
            for (int i = 0; i < idlList.Count; i++)
            {
                list2.Clear();
                foreach (var item in result)
                {
                    if (item.ClearGudaoOper.F_Id == idlList[i])
                    {
                        list2.Add(item);
                    }
                }
                //list2.OrderBy(f => f.CarGudaoIndex);
                list2.Sort((Comparison<GudaoCarEntity>)delegate (GudaoCarEntity a, GudaoCarEntity b) { return a.CarGudaoIndex > b.CarGudaoIndex ? 1 : a.CarGudaoIndex == b.CarGudaoIndex ? 0 : -1; });

                foreach (var item in list2)
                {
                    list.Add(item);
                }
            }

            #region 导出操作

            if (list != null && list.Count > 0)
            {
                List<ExcelgodaoObj> exportdata = new List<ExcelgodaoObj>();
                foreach (var Carriageobj in reuslt)
                {
                    ExcelgodaoObj temp = new ExcelgodaoObj(Carriageobj);
                    exportdata.Add(temp);
                }
                Type entityType = exportdata[0].GetType();
                PropertyInfo[] entityProperties = entityType.GetProperties();
                ArrayList listarr = new ArrayList();
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    listarr.Add(entityProperties[i].Name);
                }
                string[] arrString = (string[])listarr.ToArray(typeof(string));
                AsposeExcel.SaveFile(AsposeExcel.ListToDataTable<ExcelgodaoObj>(exportdata), "排车数据.xls", arrString);
                return null;
            }
            return null;
            #endregion
        }




        #region 车辆查询

        public ActionResult CarQueryIndex()
        {
            return View();
        }
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonCar(Pagination pagination, string gudaoName,string carId )
        {
            try
            {
                var reuslt = serverApp.GetListCar(pagination, gudaoName, carId);
                var result = (from num in reuslt
                              select new GudaoCarEntity
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  AccomplishPaicheTime = num.AccomplishPaicheTime,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.Operation.F_Id,
                                      OperationType = num.Operation.OperationType,
                                  },
                                  QiPiaoOper = num.QiPiaoOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.QiPiaoOper.F_Id,
                                      OperationType = num.QiPiaoOper.OperationType,
                                      Banci = num.QiPiaoOper.Banci,
                                      OperationTime = num.QiPiaoOper.OperationTime,
                                      OperationPersion = num.QiPiaoOper.OperationPersion == null ? null : new UserEntity()
                                      {
                                          F_Id = num.QiPiaoOper.OperationPersion.F_Id,
                                          F_RealName = num.QiPiaoOper.OperationPersion.F_RealName
                                      }
                                  },
                                  PassOper = num.PassOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.PassOper.F_Id,
                                      OperationType = num.PassOper.OperationType,
                                  }


                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }


        #endregion

       

        #region 排车登记
        public ActionResult PaiCheIndex()
        {
            var reusltkehu = CTserverApp.GetList("1");//客户
            var reusltpingming = CTserverApp.GetList("0");//品名
            ViewBag.kehulist = reusltkehu;
            ViewBag.pingminglist = reusltpingming;
            return View();
        }

        #region 获取排车总车数 和总吨数 

        /// <summary>
        /// 获取总车数 和总吨数 分页那里暂时是写死的 改的时候需改动
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword"></param>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <param name="carId"></param>
        /// <param name="customerName"></param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTotalCarInfo(Pagination pagination, string keyword, string StartTime, string EndTime, string carId, string customerName)
        {
            try
            {
                Pagination pagination2 = new Pagination();
                pagination2.sord = "AccomplishPaicheTime desc";
                pagination2.sidx = "AccomplishPaicheTime";
                pagination2.page = 1;
                pagination2.rows = 100000;
                var reuslt = serverApp.GetListPass(pagination2, keyword, StartTime, EndTime, carId, customerName);
                int carNum = 0;
                carNum = reuslt.Count;
                decimal? totalWeight = 0;
                foreach (var item in reuslt)
                {
                    totalWeight += item.Weight;
                }
                var data = new
                {
                    CarNum = carNum,
                    TotalWeight = totalWeight
                };

                return Content(data.ToJson());
            }
            catch (Exception ex)
            {
                return null;
            }

        }

        #endregion

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonPass(Pagination pagination, string keyword,string StartTime,string EndTime,string carId,string customerName)
        {
            try
            {
                var reuslt = serverApp.GetListPass(pagination, keyword, StartTime,EndTime,carId, customerName);
                var result = (from num in reuslt
                              select new GudaoCarEntity
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  AccomplishPaicheTime=num.AccomplishPaicheTime,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.Operation.F_Id,
                                      OperationType = num.Operation.OperationType,
                                  },
                                  QiPiaoOper = num.QiPiaoOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.QiPiaoOper.F_Id,
                                      OperationType = num.QiPiaoOper.OperationType,
                                      Banci = num.QiPiaoOper.Banci,
                                      OperationTime = num.QiPiaoOper.OperationTime,
                                      OperationPersion = num.QiPiaoOper.OperationPersion == null ? null : new UserEntity()
                                      {
                                          F_Id = num.QiPiaoOper.OperationPersion.F_Id,
                                          F_RealName = num.QiPiaoOper.OperationPersion.F_RealName
                                      }
                                  },
                                  PassOper = num.PassOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.PassOper.F_Id,
                                      OperationType = num.PassOper.OperationType,
                                  },
                                  ClearGudaoOper = num.ClearGudaoOper==null?null:new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.ClearGudaoOper.F_Id,
                                      IsRollBack = num.ClearGudaoOper.IsRollBack
                                  },
                                  CarGudaoIndex = num.CarGudaoIndex
                              }).ToList();
                //排序
                List<GudaoCarEntity> list = new List<GudaoCarEntity>();
                List<GudaoCarEntity> list2=new List<GudaoCarEntity>();
                List<string> idlList=new List<string>();
                string fid="";
                foreach (var item in result)
                {
                    if (item.ClearGudaoOper.F_Id != fid)
                    {
                        fid = item.ClearGudaoOper.F_Id;
                        idlList.Add(item.ClearGudaoOper.F_Id);
                    }
                }
                for (int i = 0; i < idlList.Count; i++)
                {
                    list2.Clear();
                    foreach (var item in result)
                    {
                        if (item.ClearGudaoOper.F_Id == idlList[i])
                        {
                            list2.Add(item);
                        }
                    }
                    //list2.OrderBy(f => f.CarGudaoIndex);
                    list2.Sort((Comparison<GudaoCarEntity>)delegate (GudaoCarEntity a, GudaoCarEntity b) { return a.CarGudaoIndex > b.CarGudaoIndex? 1 : a.CarGudaoIndex == b.CarGudaoIndex? 0 : -1; });

                    foreach (var item in list2)
                    {
                        list.Add(item);
                    }
                }

                var data = new
                {
                    rows = list,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }

        #endregion


        #region 接车登记  暂时没用

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonArrive(Pagination pagination, string keyValue)
        {
            try
            {
                var reuslt = serverApp.GetListArrive(pagination, keyValue);
                var result = (from num in reuslt
                              select new GudaoCarEntity
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.Operation.F_Id,
                                  }
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }

        #endregion

        #region 回收重装

        public ActionResult RecoveryIndex()
        {
            return View();
        }

        public ActionResult OnRecovery(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (string.IsNullOrEmpty(keyValue))
            {
                return Error("没有获取到车辆Id");
            }
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var res = serverApp.OnRecoveryCar(keyValue);
                if (res >= 0)
                {
                    return Success("卸后利用成功");
                }
                else if (res == -1)
                {
                    return Error("没有获取到车辆信息");
                }
                else if (res == -2)
                {
                    return Error("该车已经卸后利用不能再次进行该操作");
                }
                else if (res == -3)
                {
                    return Error("插入卸后利用操作数据失败");
                }
                else if (res == -4)
                {
                    return Error("更新车辆数据失败");
                }
                else
                {
                    return Error("");
                } 
            }
            else
            {
                return Success("登录人员不是调度员或超级管理员");
            }
           
        }

        [HttpGet] 
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonRecovery(Pagination pagination, string keyword, string StartTime, string EndTime, string carId)
        {
            try
            {
                var reuslt = serverApp.GetListRecovery(pagination, keyword, StartTime, EndTime, carId);
                var result = (from num in reuslt
                              select new
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  RecoveryOper = num.RecoveryOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.RecoveryOper.F_Id,
                                      Banci = posService.GetForm(f => f.F_Id == num.RecoveryOper.Banci).BanciUser,
                                      OperationTime = num.RecoveryOper.F_CreatorTime,
                                      OperationPersion = userApp.GetForm(num.RecoveryOper.F_CreatorUserId)
                                  },
                                  RecoveryTime = num.RecoveryTime
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }

        #endregion

        #region 起票登记

        public ActionResult QiPiaoIndex()
        {
            var reusltkehu = CTserverApp.GetList("1");//客户
            var reusltpingming = CTserverApp.GetList("0");//品名
            ViewBag.kehulist = reusltkehu;
            ViewBag.pingminglist = reusltpingming;
            return View();
        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonQiPiao(Pagination pagination, string keyword,string StartTime,string EndTime,string carId,string arriveStation,string customerName,string pingming)
        {
            try
            {
                var reuslt = serverApp.GetListQiPiao(pagination, keyword,StartTime,EndTime,carId, arriveStation,customerName,pingming);
                var result = (from num in reuslt
                              select new 
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.Operation.F_Id,
                                      OperationType = num.Operation.OperationType,
                                  },
                                  QiPiaoOper = num.QiPiaoOper==null?null:new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.QiPiaoOper.F_Id,
                                      OperationType=num.QiPiaoOper.OperationType,
                                      Banci = posService.GetForm(f=>f.F_Id== num.QiPiaoOper.Banci).BanciUser,
                                      OperationTime = num.QiPiaoOper.OperationTime,
                                      //OperationPersion= num.QiPiaoOper.OperationPersion==null?null:new UserEntity()
                                      //{
                                      //    F_Id = num.QiPiaoOper.OperationPersion.F_Id,
                                      //    F_RealName = num.QiPiaoOper.OperationPersion.F_RealName
                                      //}
                                      OperationPersion = userApp.GetForm(num.QiPiaoOper.F_CreatorUserId)
                                  },
                                  QiPiaoTime=num.QiPiaoTime,
                                  PassOper = num.PassOper==null?null:new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.PassOper.F_Id,
                                      OperationType = num.PassOper.OperationType,
                                  }

                                   
                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }


        public ActionResult GetExportQipiaoExcell(string keyword, string StartTime, string EndTime, string carId, string arriveStation, string customerName, string pingming) {
            Pagination pagination2 = new Pagination();
            pagination2.sord = "ArriveDateTime desc";
            pagination2.sidx = "ArriveDateTime";
            pagination2.page = 1;
            pagination2.rows = 1000000000;
            var reuslt = serverApp.GetListQiPiao(pagination2, keyword, StartTime, EndTime, carId, arriveStation, customerName, pingming);
            if (reuslt != null && reuslt.Count > 0) {
                List<ExcelGudaoCarObj> exportdata = new List<ExcelGudaoCarObj>();
                foreach (var Carriageobj in reuslt)
                {
                    ExcelGudaoCarObj temp = new ExcelGudaoCarObj(Carriageobj);
                    exportdata.Add(temp);
                }
                Type entityType = exportdata[0].GetType();
                PropertyInfo[] entityProperties = entityType.GetProperties();
                ArrayList list = new ArrayList();
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    list.Add(entityProperties[i].Name);
                }
                string[] arrString = (string[])list.ToArray(typeof(string));
                ZFine.Code.AsposeExcel.SaveFile(ZFine.Code.AsposeExcel.ListToDataTable<ExcelGudaoCarObj>(exportdata), "排车数据.xls", arrString);
                return null;
            }
            return null;
        }
        #endregion


        #region 股道上的车辆查询


        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetGridJsonGudaoCar(Pagination pagination, string keyValue)
        {
            try
            {
                //将url解码
                Encoding utf8 = Encoding.UTF8;     
                string code = HttpUtility.UrlDecode(keyValue.ToUpper(), utf8);
                string tempFenhao = "";
                var reuslt = serverApp.GetList(pagination, code);
                var result = (from num in reuslt
                    select new GudaoCarEntity
                    {
                        F_Id = num.F_Id,
                        BelongCarriage=num.BelongCarriage==null?null:new CarriageEntity()
                        {
                            F_Id = num.BelongCarriage.F_Id,
                            CarriageID = num.BelongCarriage.CarriageID,
                            CarType = num.BelongCarriage.CarType,
                            Capacity=num.BelongCarriage.Capacity
                        },
                        ThingsName=num.ThingsName,
                        ArriveStation = num.ArriveStation,
                        UnitNumber = num.UnitNumber,
                        Weight = num.Weight,
                        Customers = num.Customers,
                        isAccomplishStatistics=num.isAccomplishStatistics,
                        IsAccomplishPermitThrough=num.IsAccomplishPermitThrough,
                        isAccomplishPaiche=num.isAccomplishPaiche,
                        IsAccomplishduiwei=num.IsAccomplishduiwei,
                        isQiPiao = num.isQiPiao,
                        ArriveDateTime = num.ArriveDateTime,
                        Remark = num.Remark,
                        CarGudaoIndex=num.CarGudaoIndex,
                        BelongPathway = num.BelongPathway==null?null:new PathwayEntity()
                        {
                            F_Id = num.BelongPathway.F_Id,
                            Name=num.BelongPathway.Name,
                            IsCounterpoint=num.BelongPathway.IsCounterpoint
                        },
                        Operation = num.Operation==null?null:new OperationGudaoCarEntity()
                        {
                            F_Id = num.Operation.F_Id,
                        },
                        CarBoxStr= carriageBoxApp.GetCarBoxCodes(num.BelongCarriage,ref tempFenhao),
                        Fenhao = string.IsNullOrEmpty(tempFenhao)? num.Fenhao: tempFenhao
                    }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }

        }

        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = serverApp.GetForm(keyValue);
            return Content(data.ToJson());
        }

        /// <summary>
        /// 统计运量
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult Statistic(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" ||LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.YunLiang(keyValue);
                if (result>0)
                {
                    return Success("统计运量成功");
                }
                else if(result==-1)
                {
                    return Error("插入操作数据失败！");
                }
                else if (result == -2)
                {
                    return Error("修改车辆信息失败！");
                }
                else if (result == -3)
                {
                    return Error("车辆信息错误！请刷新列表！");
                }
                else if (result == -4)
                {
                    return Error("已经统计过运量不能再次统计！！");
                }
                else if (result == -5)
                {
                    return Error("存在人员工资系数未填写的情况！请检查后再操作！");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }
          
        }

        /// <summary>
        /// 取消统计运量方法
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult CancelStatistic(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.CancelYunLiang(keyValue);
                if (result > 0)
                {
                    return Success("取消统计运量成功");
                }
                else if (result == -1)
                {
                    return Error("修改工资数据或操作数据失败！");
                }
                else if (result == -2)
                {
                    return Error("修改车辆信息失败！");
                }
                else if (result == -3)
                {
                    return Error("车辆信息错误！没有找到对应车辆！请刷新列表！");
                }
                else if (result == -4)
                {
                    return Error("该车没有统计过运量不能取消！！");
                }
                else if (result == -5)
                {
                    return Error("当前班次与统计运量班次不对应！不能进行此操作！！");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }

        /// <summary>
        /// 对位
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult Duiwei(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.Duiwei(keyValue);
                if (result > 0)
                {
                    return Success("统计对位成功");
                }
                else if (result == -1)
                {
                    return Error("插入操作数据失败！");
                }
                else if (result == -2)
                {
                    return Error("修改车辆信息失败！");
                }
                else if (result == -3)
                {
                    return Error("车辆信息错误！请刷新列表！");
                }
                else if (result == -4)
                {
                    return Error("已经对位不能再次对位！！");
                }
                else if (result == -5)
                {
                    return Error("存在人员工资系数未填写的情况！请检查后再操作！");
                }
                else if (result == -6)
                {
                    return Error("该股道没有开启对位属性 不能进行对位操做！");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }
        /// <summary>
        /// 取消对位
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult CancelDuiwei(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.CancelDuiwei(keyValue);
                if (result > 0)
                {
                    return Success("取消对位成功");
                }
                else if (result == -1)
                {
                    return Error("修改工资数据或操作数据失败！");
                }
                else if (result == -2)
                {
                    return Error("修改车辆信息失败！");
                }
                else if (result == -3)
                {
                    return Error("车辆信息错误！没有找到对应车辆！请刷新列表！");
                }
                else if (result == -4)
                {
                    return Error("该车没有对位不能取消！！");
                }
                else if (result == -5)
                {
                    return Error("当前班次与对位班次不对应！不能进行此操作！！");
                }
                else if(result ==-6)
                {
                    return Error("该股道没有开启对位功能不能进行此操作!");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }
        }

        /// <summary>
        /// 起票方法
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult QiPiao(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.QiPiao(keyValue);
                if (result > 0)
                {
                    return Success("起票成功");
                }
                else if (result == -1)
                {
                    return Error("没有找到对应车辆！");
                }
                else if (result == -2)
                {
                    return Error("封号为空！请先填写封号");
                }
                else if (result == -3)
                {
                    return Error("该车已经起票！不能再次起票！");
                }
                else if (result == -4)
                {
                    return Error("插入起票数据时发生错误！请检查数据库是否连接正常！");
                }
                else if (result == -5)
                {
                    return Error("更新车辆数据失败！请检查数据库是否连接正常！！");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }

        /// <summary>
        /// 取消起票方法
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult CancelQiPiao(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.CancelQiPiao(keyValue);
                if (result > 0)
                {
                    return Success("撤销起票成功");
                }
                else if (result == -1)
                {
                    return Error("没有找到对应车辆！");
                }
                else if (result == -2)
                {
                    return Error("该车已放行！请先取消放行再进行取消起票操作！");
                }
                else if (result == -3)
                {
                    return Error("该车没有起票！不能进行取消起票操作！");
                }
                else if (result == -4)
                {
                    return Error("插入取消起票数据时发生错误！请检查数据库是否连接正常！");
                }
                else if (result == -5)
                {
                    return Error("更新车辆数据失败！请检查数据库是否连接正常！！");
                }
                else if (result == -6)
                {
                    return Error("当前班次与起票时的班次不对应！不能进行此操作!");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }

        /// <summary>
        /// 放行的方法
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult Pass(string keyValue,string perKeyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.Pass(keyValue,perKeyValue);
                if (result > 0)
                {
                    return Success("统计放行成功");
                }
                else if (result == -1)
                {
                    return Error("车辆信息不正确！");
                }
                else if (result == -2)
                {
                    return Error("货运员未选择！请选择货运员再进行此操作！");
                }
                else if (result == -3)
                {
                    return Error("该车已经放行！不能再次进行放行操作！");
                }
                else if (result == -40)
                {
                    return Error("该品名当前禁止放行！");
                }
                else if (result == -4)
                {
                    return Error("插入员工放行数据失败！请检查数据库是否连接正常！");
                }
                else if (result == -5)
                {
                    return Error("更新车辆数据失败！请检查数据库是否连接正常！！");
                }
                else if (result == -6)
                {
                    return Error("货运员的单价未填写！请联系管理员检查人员信息");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }

        /// <summary>
        /// 取消放行的方法
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult CancelPass(string keyValue)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.CancelPass(keyValue);
                if (result > 0)
                {
                    return Success("取消放行成功");
                }
                else if (result == -1)
                {
                    return Error("车辆信息不正确！");
                }
                else if (result == -2)
                {
                    return Error("当前班次与放行时的班次不对应！不能进行此操作！");
                }
                else if (result == -3)
                {
                    return Error("该车未放行！不能进行取消放行操作！");
                }
                else if (result == -4)
                {
                    return Error("插入员工放行数据失败！请检查数据库是否连接正常！");
                }
                else if (result == -5)
                {
                    return Error("更新车辆数据失败！请检查数据库是否连接正常！！");
                }
                else
                {
                    return Error("发生未知错误！");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员！不能进行该操作！");
            }

        }
        #endregion

        /// <summary>
        /// 修改股道car车辆信息
        /// </summary>
        /// <param name="ServerEntity"></param>
        /// <param name="keyValue"></param>
        /// <param name="carType"></param>
        /// <returns></returns>
        public ActionResult SubmitForm(GudaoCarEntity ServerEntity, string keyValue,string carType,string CarriageID)
        {
            var LoginInfo = OperatorProvider.Provider.GetCurrent();
            string cap = Request.QueryString["Capacity"];
            string carriageID = Request.QueryString["CarriageID"];
            if (string.IsNullOrEmpty(cap))
            {
                cap = "0";
            }
            decimal Capacity = Convert.ToDecimal(cap);
            if (FindGangWei(LoginInfo.UserId) == "调度员" || LoginInfo.UserName == "超级管理员")
            {
                var result = serverApp.SubmitForm(ServerEntity, keyValue, Capacity,carType, carriageID);
                if (result > 0)
                {
                    return Success("操作成功！");
                }
                else if(result==-1)
                {
                    return Error("获取车辆信息失败");
                }
                else if (result == -2)
                {
                    return Error("货物超载");
                }
                else if (result == -10)
                {
                    return Error("要修改的车号已存在");
                }
                else
                {
                    return Error("发生未知错误");
                }
            }
            else
            {
                return Error("当前登录人员不是调度员或超级管理员！不能进行该操作！");
            }
        }


        /// <summary>
        /// 选择放行人员
        /// </summary>
        /// <returns></returns>
        public ActionResult SelectPer()
        {
            //获取当前上班的人员
            var positionrecord = positoonapp.GetForm(f => f.State == true);
            var huoyunyuan1 = userApp.GetForm(positionrecord.HuoyunyunaUser1.F_Id);
            var huoyunyuan2 = userApp.GetForm(positionrecord.HuoyunyunaUser2.F_Id);
            List<UserEntity> userList = new List<UserEntity>();
            userList.Add(huoyunyuan1);
            userList.Add(huoyunyuan2);
            ViewBag.userList = userList.ToJson();
            return View();
        }


        /// <summary>
        /// 放行
        /// </summary>
        /// <returns></returns>
        public ActionResult FangXingSelectIndex()
        {
            return View();
        }
        /// <summary>
        /// 放行查询（仅仅用来查人 不算工资）
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="keyword">车号</param>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult FangXingSelectDetailIndex(Pagination pagination, string keyword, string StartTime, string EndTime,string carId)
        {
            try
            {
                var reuslt = serverApp.GetListFangXing(pagination, keyword, StartTime, EndTime, carId);
                var result = (from num in reuslt
                              select new
                              {
                                  F_Id = num.F_Id,
                                  BelongCarriage = num.BelongCarriage == null ? null : new CarriageEntity()
                                  {
                                      F_Id = num.BelongCarriage.F_Id,
                                      CarriageID = num.BelongCarriage.CarriageID,
                                      CarType = num.BelongCarriage.CarType,
                                      Capacity = num.BelongCarriage.Capacity
                                  },
                                  ThingsName = num.ThingsName,
                                  Fenhao = num.Fenhao,
                                  ArriveStation = num.ArriveStation,
                                  UnitNumber = num.UnitNumber,
                                  Weight = num.Weight,
                                  Customers = num.Customers,
                                  isAccomplishStatistics = num.isAccomplishStatistics,
                                  IsAccomplishPermitThrough = num.IsAccomplishPermitThrough,
                                  AccomplishPermitThroughTime=num.AccomplishPermitThroughTime,
                                  isAccomplishPaiche = num.isAccomplishPaiche,
                                  IsAccomplishduiwei = num.IsAccomplishduiwei,
                                  isQiPiao = num.isQiPiao,
                                  ArriveDateTime = num.ArriveDateTime,
                                  Remark = num.Remark,
                                  BelongPathway = num.BelongPathway == null ? null : new PathwayEntity()
                                  {
                                      F_Id = num.BelongPathway.F_Id,
                                      Name = num.BelongPathway.Name
                                  },
                                  Operation = num.Operation == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.Operation.F_Id,
                                      OperationType = num.Operation.OperationType,
                                  },
                                  QiPiaoOper = num.QiPiaoOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.QiPiaoOper.F_Id,
                                      OperationType = num.QiPiaoOper.OperationType,
                                      Banci = posService.GetForm(f => f.F_Id == num.QiPiaoOper.Banci).BanciUser,
                                      OperationTime = num.QiPiaoOper.OperationTime,
                                      //OperationPersion= num.QiPiaoOper.OperationPersion==null?null:new UserEntity()
                                      //{
                                      //    F_Id = num.QiPiaoOper.OperationPersion.F_Id,
                                      //    F_RealName = num.QiPiaoOper.OperationPersion.F_RealName
                                      //}
                                      OperationPersion = userApp.GetForm(num.QiPiaoOper.F_CreatorUserId)
                                  },
                                  QiPiaoTime = num.QiPiaoTime,
                                  PassOper = num.PassOper == null ? null : new OperationGudaoCarEntity()
                                  {
                                      F_Id = num.PassOper.F_Id,
                                      OperationType = num.PassOper.OperationType,
                                      OperationTime = num.PassOper.OperationTime,
                                      Banci = posService.GetForm(f => f.F_Id == num.PassOper.Banci).BanciUser,
                                      OperationPersion=userApp.GetForm(num.PassOper.F_CreatorUserId)
                                  }


                              }).ToList();
                var data = new
                {
                    rows = result,
                    total = pagination.total,
                    page = pagination.page,
                    records = pagination.records
                };
                return Content(data.ToJson());

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        
       

    }

    public class ExcelgodaoObj {
        public string 车号 { get; set; }
        public string 车型 { get; set; }
        public string 载重 { get; set; }
        public string 所属股道 { get; set; }
        public string 品名 { get; set; }
        public string 封号 { get; set; }
        public string 到站 { get; set; }
        public string 件数 { get; set; }
        public string 重量 { get; set; }
        public string 客户 { get; set; }
        public string 排车时间 { get; set; }
        public string 备注 { get; set; }

        public ExcelgodaoObj(GudaoCarEntity gudaoobj) {
            if (gudaoobj.BelongCarriage != null)
            {
                this.车号 = gudaoobj.BelongCarriage.CarriageID;
                this.车型 = gudaoobj.BelongCarriage.CarType;
                this.载重 = gudaoobj.BelongCarriage.Capacity.ToString();
            }
            else
            {
                this.车号 = "未填写";
                this.车型 = "未填写";
                this.载重 = "未填写";
            }
            if (gudaoobj.BelongPathway != null)
            {
                this.所属股道 = gudaoobj.BelongPathway.Name;
            }
            else {
                this.所属股道 = "无";
            }
            this.品名 = gudaoobj.ThingsName;
            this.封号 = gudaoobj.Fenhao;
            this.到站 = gudaoobj.ArriveStation;
            if (gudaoobj.UnitNumber != null)
            {
                this.件数 = gudaoobj.UnitNumber.ToString();
            }
            else {
                this.件数 = "无";
            }
            
            if (gudaoobj.Weight != null)
            {
                this.重量 = gudaoobj.Weight.ToString();
            }
            else {
                this.重量 = "无";
            }
           
            this.客户 = gudaoobj.Customers;
            this.排车时间 = Convert.ToDateTime(gudaoobj.AccomplishPaicheTime).ToString("yyyy-MM-dd HH:mm:dd");
            this.备注 = gudaoobj.Remark;
        }
    }

}